package com.app.auth.service;

import com.app.auth.advice.AuthFilter;
import com.app.auth.entity.RoleInfo;
import com.app.auth.entity.User;
import com.app.auth.entity.UserInfo;
import com.app.auth.enums.Events;
import com.app.auth.enums.States;
import com.app.auth.repository.RoleInfoRepository;
import com.app.auth.repository.UserInfoRepository;
import com.app.auth.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.statemachine.StateMachine;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HomeService {
    @Autowired
    private UserInfoRepository userInfoRepository;

    @Autowired
    private RoleInfoRepository roleInfoRepository;

    @Autowired
    private UserRepository userRepository;


    public Integer userInfo(UserInfo userInfo) {
        userInfoRepository.save(userInfo);
        return userInfo.getId();
    }

    @AuthFilter
    public List<UserInfo> userInfos() {
        service.get();
        return userInfoRepository.findAll();
    }

    public List<RoleInfo> roleInfos() {
        return roleInfoRepository.findAll();
    }


    public Integer user(User user) {
        userRepository.save(user);
        return user.getId();
    }

    @Autowired
    private StateMachine<States,Events> stateMachine;

    public Boolean stateMachine(Events events){
        stateMachine.start();
        stateMachine.sendEvent(events);
        return true;
    }

    @Autowired
    private IService service;

}
